<!DOCTYPE HTML>
<html lang="cn" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <%include("/common/header.html",{title:''}){}%>
    <title>消息管理</title>
    <style>
    </style>
</head>
<body>

<div class="x-nav" style="margin: 20px auto auto 15px;">
      <span class="layui-breadcrumb" lay-filter="breadcrumb" style="visibility: visible;">
        <a href="javascript:;">首页</a><span lay-separator="">/</span>
        <a href="javascript:;">系统设置</a><span lay-separator="">/</span>
        <a><cite>消息管理</cite></a>
      </span>
    <a class="layui-btn layui-btn-primary layui-btn-small" style="margin: -5px 14px 0px 0px;float:right;" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon layui-icon-refresh-3"></i>
    </a>
</div>


<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="LAY-user-manage" lay-filter="LAY-user-manage"></table>
            <script type="text/html" id="toolbarDemo">
                <button class="layui-btn layuiadmin-btn-useradmin" lay-event="delete">删除</button>
                <button class="layui-btn layuiadmin-btn-useradmin" lay-event="add">添加</button>
            </script>
            <script type="text/html" id="imgTpl">
                <img style="display: inline-block; width: 50%; height: 100%;" src={{ d.avatar }}>
            </script>
            <script type="text/html" id="table-useradmin-webuser">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>
<!-- 弹出页面 -->
<div class="layui-form" lay-filter="layuiadmin-form-useradmin"
     id="layuiadmin-form-useradmin"
     style="padding: 15px;display: none">
    <form action class="layui-form" lay-filter="component-form-element" id="myform">
        <input type="hidden" name="id">
        <div class="layui-form-item">
            <label class="layui-form-label">消息类型</label>
            <div class="layui-input-block">
                <input  type="radio" name="type" value="1" title="系统消息" checked="">
                <div class="layui-unselect layui-form-radio layui-form-radioed"><i
                        class="layui-anim layui-icon"></i>
                    <div>系统消息</div>
                </div>
                <input  type="radio" name="type" value="2" title="促销消息">
                <div class="layui-unselect layui-form-radio"><i class="layui-anim layui-icon"></i>
                    <div>促销消息</div>
                </div>
                <!--<input  type="radio" name="typeId" value="2" title="客服助手">-->
                <!--<div class="layui-unselect layui-form-radio"><i class="layui-anim layui-icon"></i>-->
                <!--<div>客服助手</div>-->
                <!--</div>-->


            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label">标题</label>
            <div class="layui-input-inline">
                <input type="text" name="title" lay-verify="name" value=""  class="layui-input"  placeholder="请输入标题">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">内容</label>
            <div class="layui-input-inline">
                <input type="text" name="content" lay-verify="address" value=""  class="layui-input"  placeholder="请输入消息内容">
            </div>
        </div>





        <div class="layui-form-item">
            <label class="layui-form-label">链接</label>
            <div class="layui-input-inline">
                <input type="text" name="link" lay-verify="lat" value=""  class="layui-input"  placeholder="请输入链接">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">图片 &nbsp;</label>
                <div class="layui-input-inline" style="width: 150px;">
                    <!--<input type="hidden" id="hid">-->
                    <button type="button" class="layui-btn" id="upload1">上传图片</button>
                    <div class="layui-upload-list">
                        <img class="layui-upload-img" width="100px" height="80px" id="img1"/>
                        <p id="demoText"></p>
                    </div>
                </div>
            </div>
        </div>



        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="component-form-element">立即提交</button>

            </div>
        </div>
    </form>
</div>

<%include("/common/foot.html"){}%>
<script>
    layui.use(['index', 'table', 'form', 'laydate', 'layedit', 'AjaxCommon', 'AliangLayui'], function () {
        var $ = layui.$;
        var table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        var AjaxCommon = layui.AjaxCommon;
        var AliangLayui = layui.AliangLayui;

        var title = "添加轮播图";
        var size = ['600px', '500px']
        var findAll_url='system/message/findAll.json';
        var update_url='system/message/update.jhtml';
        var delete_url='system/message/delete.jhtml';

        var cols = [ //表头
            {checkbox: true,fixed:true},
            {field: 'type', width:"100", title: '消息类型',templet:"<div>{{ d.type==1?'系统消息':'促销消息' }}</div>",fixed:'left'},
            {field: 'title', width:"160", title: '标题'},
            {field: 'content', width:"250", title: '消息内容'},
            {field: 'createDate', width:"180", templet:"<div>{{layui.util.toDateString(d.createDate, 'yyyy-MM-dd HH:mm:ss')}}</div>",title: '发布时间'},
            {field: 'link', width:"150", title: '链接'},
            {field:'picture',width: "110", title: '缩略图',templet:"<div><img src={{d.picture}} style='width:30px;height:30px'></div>"},
            {title:'操作', width:147, toolbar: '#table-useradmin-webuser',fixed:'right'}
        ];
        table.render({
            elem: '#LAY-user-manage',
            id: 'listReload',
            toolbar: '#toolbarDemo',
            title: title,
            url: findAll_url,
            page: true, //开启分页
            limits: [10, 20, 30, 50], //控制多少行一页（默认五条一页）
            defaultToolbar: ['filter', 'print', 'exports'],
            cols: [cols],
            done() {
                //alert() 数据渲染完的方法
            }
        });



        //初始化修改页面

        //初始化修改页面
        function loadData(data){
            var ret={
                "id": data.id,
                "type":data.type,
                "title": data.title,
                "content": data.content,
                "createDate": data.createDate,
                "link": data.link,
                "picture": data.picture,

            }
            $("#img1").attr("src",data.picture);
            //$("#hid").val(data.picture);
            //alert($("#hid").val());
            return ret;
        }
        //图片上传
        layui.use('upload', function () {
            var $ = layui.jquery, upload = layui.upload;
            var uploadInst = upload.render({
                elem: '#upload1',
                auto: false,
                choose: function (obj) {
                    obj.preview(function (index, file, result) {//预读本地文件示例，不支持ie8
                        console.log(JSON.stringify(obj));
                        console.log(JSON.stringify(file));

                        $('#img1').attr('src', result); //图片链接（base64）
                    });
                }
            });
        });

        //初始化日期模块
        laydate.render({
            elem: '#create' //指定元素
        });
        //////////////////////////////////////以上是修改每个页面修改的地方
        //弹出修改页面调用初始化数据方法
        function initUpdate(data){
            //alert(data.id);
            //table.reload('listReload');
            layer.open({
                title: title,
                area: size,
                type: 1,
                content: $('#layuiadmin-form-useradmin'), //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                btn: ['关闭'] //可以无限个按钮
            });
            if(data==0 || data==null){
                //$(":reset").click()
                $('#myform')[0].reset()
                return;
            }
            layui.form.val("component-form-element",loadData(data));
        }
        //修改页面的内容提交到后台处理
        form.on('submit(component-form-element)', function(data){
            postUpdate(data);
            return false;
        });
        //头工具栏事件
        table.on('toolbar(LAY-user-manage)', function(obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'add':
                    initUpdate(0);
                    break;
                case 'delete':
                    var data = checkStatus.data;
                    if (data.length > 0) {
                        //var id = new Array(); //声明数组
                        for (var i in data) {
                            var a = data[i].id;//得到复选框的值
                            postDelete(a);
                        }
                        /*layer.confirm('删除以后数据将不能恢复，确定删除吗？', function(index) {
                            //alert(data.id);
                            postDelete(id);
                        });*/
                        //alert(id)
                        //layer.alert(JSON.stringify(data));
                        //layer.close(index);
                    } else {
                        layer.msg("至少选择一个进行删除");
                    }
                    break;
                case 'export':
                    layer.msg("导出");
                    break;
            }
        });

        //点击每行右工具栏“操作”里面按钮效果
        table.on('tool(LAY-user-manage)', function(obj) {
            var data = obj.data;
            if (obj.event === 'detail') {
                layer.msg('ID：' + data.id + ' 的查看操作');
            } else if (obj.event === 'edit') {
                initUpdate(data);
            } else if (obj.event === 'del') {
                layer.confirm('删除以后数据将不能恢复，确定删除吗？', function(index) {
                    //alert(data.id);
                    postDelete(data.id);
                });
            }
        });


        //监听搜索按钮效果
        form.on('submit(LAY-user-front-search)', function(data) {
            var field = data.field;
            table.reload('listReload', {
                where :{
                    name: field.name,
                    price: field.price,
                    pay: field.pay
                },
                page: {
                    curr: 1
                }
            });
        });

        //ajax提交删除内容
        window.postDelete = function (id){
            $.ajax({
                url : delete_url,
                type : "POST",
                data : {
                    "id" : id
                },
                dataType : "json",
                success : function(ret) {
                    if (ret == null) {
                        layer.msg("删除失败", {
                            icon : 5
                        });
                    } else {
                        layer.msg("删除成功", {
                            icon : 6
                        });
                        layer.closeAll();
                        table.reload('listReload');
                    }
                    return ret;
                },
                error:function(jqXHR, textStatus, errorThrown){
                    var estr="Page:"+window.location.pathname+",Function: postDelete()";
                    layer.open({
                        title: "错误信息",
                        area: ['600px', '600px'],
                        type: 2,
                        content: "base/error.jhtml?info="+estr, //
                        btn: ['关闭'] //可以无限个按钮
                    });
                }
            });
        }

        //ajax提交修改内容
        window.postUpdate = function (data){
            var formData = new FormData();
            // 利用append的内置方式
            formData.append("data", JSON.stringify(data.field));
            //formData.append("id", JSON.stringify(data.id));
            formData.append("file", $("input[name='file']")[0].files[0]);
            $.ajax({
                url: update_url,
                type: "POST",
                //traditional:true,
                data: formData,
                processData: false,
                contentType: false,
                success: function (data) {
                    if (data.code == 0) {
                        layer.closeAll();
                        layer.msg(data.msg, {time: 600, icon: 1}, function () {
                            //table.reload('listReload');
                            location.replace(location.href);
                        });
                    } else {
                        layer.msg(data.msg, {icon: 5});
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    var estr="Page:"+window.location.pathname+",Function:postUpdate()";
                    layer.open({
                        title: "错误信息",
                        type: 2,
                        content:  "base/error.jhtml?info="+estr,
                        btn: ['关闭'] //可以无限个按钮
                    });
                }
            });
        }
    });

</script>
</body>

</html>